package xyz.scootaloo.code.common

import kotlin.math.min

/**
 * 根据某种规则对集合内容排序
 *
 * @author flutterdash@qq.com
 * @since 2021/10/8 12:08
 */

fun MutableList<String>.sortByDict(): MutableList<String> {
    return this.apply {
        this.sortWith { s1, s2 ->
            val minLen = min(s1.length, s2.length)
            for (i in 0 until minLen) {
                if (s1[i].code == s2[i].code)
                    continue
                if (s1[i].code > s2[i].code)
                    return@sortWith 1
                else
                    return@sortWith -1
            }
            return@sortWith if (s1.length > s2.length) 1 else -1
        }
    }
}

fun main() {
    val list = mutableListOf("b", "c", "aab", "aa")
    list.sortByDict()
    println(list)
}
